using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameCore : MonoBehaviour
{

    public enum PieceType
    {
        NORMAL,
        COUNT,
    }
    [System.Serializable]
    public struct PiecePrefab
    {
        public PieceType type;
        public GameObject prefab;
    }

    public int xColumn = 6;
    public int yRow = 6;

    public MapTile[,] mapTiles;

    private Vector2 initPos;
    private int tileSize = 100;
    public GameObject tilePrefab;
    public Transform groupMap;

    public CandySymbol[,] pieces;
    public Dictionary<PieceType, GameObject> piecePrefabDict;
    public PiecePrefab[] piecePrefabs;
    public CandyView[,] candyViews;

    public Transform groupCandy;


    private void Awake()
    {
        piecePrefabDict = new Dictionary<PieceType, GameObject>();
        for (int i = 0; i < piecePrefabs.Length; i++)
        {
            piecePrefabDict.Add(piecePrefabs[i].type, piecePrefabs[i].prefab);
        }


        initPos = new Vector2(-250, -250);
        mapTiles = new MapTile[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                GameObject go = Instantiate(tilePrefab);
                go.transform.SetParent(groupMap);
                go.transform.localPosition = new Vector2(initPos.x + (x * tileSize), initPos.y + (y * tileSize));
                go.transform.localScale = Vector2.one;

                MapTile mt = new MapTile();
                mt.Init(x, y, go);
                mapTiles[x, y] = mt;
            }
        }
        candyViews = new CandyView[xColumn, yRow];
        pieces = new CandySymbol[xColumn, yRow];
        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                GameObject go = Instantiate(piecePrefabDict[PieceType.NORMAL]);
                go.transform.SetParent(groupCandy);
                go.transform.localPosition = Vector2.zero;//new Vector2(mapTiles[x,y].XPos,mapTiles[x,y].YPos);
                go.transform.localScale = Vector2.one;

                CandyView cv = new CandyView();
                cv.Init(x, y, go);
                candyViews[x, y] = cv;

                CandySymbol cs =new CandySymbol();
                cs.Init(x, y, this, PieceType.NORMAL);
                pieces[x, y] = cs;
                if (cs.IsMovable())
                {
                    cs.MoveCandy.Move(x, y);
                }
            }
        }



    }

    void Start()
    {
      







    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
